<template>
  <el-popover
    placement="right"
    trigger="hover"
    popper-class="popover"
    :offset="30"
  >
    <template #reference>
      <div class="trigger">
        <span class="text">
          {{ gradeMap[nowGlobalStageId]
          }}{{ subjectMap[nowGlobalSubjectId] }}</span
        >
        <i class="i-switch"></i>
      </div>
    </template>
    <div class="tab-list">
      <template v-for="item in globalSubjectListYouSee" :key="item.key">
        <div class="item" v-if="item.value?.length">
          <template v-if="item.value.length">
            <div class="grade-name">{{ gradeMap[item.key] }}:</div>
            <div class="subject-list">
              <div
                class="subject"
                v-for="subject in item.value"
                :key="subject.subject_code"
                :class="{
                  active:
                    subject.subject_num === nowGlobalSubjectId &&
                    item.key === nowGlobalStageId,
                  special: subject.subject_name.length > 3
                }"
                @click="onClickSubject(subject, item.key)"
              >
                {{ subject.subject_name }}
              </div>
            </div>
          </template>
        </div>
      </template>
    </div>
  </el-popover>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { storeToRefs } from 'pinia'
import { useAuthStore } from '@/stores/auth-store'
import { subjectMap } from '@/constants'
import { useTaskStore } from '@/stores/task'

import { getCourseId } from '@/apis'

const authStore = useAuthStore()
const taskStore = useTaskStore()
const { changeGlobalInfo } = authStore
const { globalSubjectListYouSee, nowGlobalSubjectId, nowGlobalStageId } =
  storeToRefs(authStore)

const gradeMap = {
  1: '小学',
  2: '初中',
  3: '高中'
}

const popoverVisible = ref(false)

function closePopover() {
  popoverVisible.value = false
}

async function onClickSubject(subject, gradeId) {
  console.log(subject, nowGlobalSubjectId.value)

  if (
    subject.disabled ||
    (subject.subject_num === nowGlobalSubjectId.value &&
      subject.section_num === nowGlobalStageId)
  ) {
    return
  }
  console.log(subject, gradeId)

  changeGlobalInfo(subject.subject_num, gradeId)
  // 请求courseId
  const res = await getCourseId({
    subjectId: subject.subject_num,
    stageId: subject.section_num
  })
  console.log(res.data)
  taskStore.getBasketQuestionInfo()
}
taskStore.getBasketQuestionInfo()

onBeforeUnmount(() => {
  window.removeEventListener('click', closePopover)
})
</script>
<!-- eslint-disable -->
<style lang="less">
.popover {
  width: 300px !important;
  padding: 10px 20px !important;
  border-radius: 10px;
  .special {
    grid-column-start: span 2;
  }
}
</style>

<style lang="less" scoped>
.trigger {
  box-sizing: border-box;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: @radius-component;
  width: 150px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;

  .text {
    font-size: @size-big;
    color: #333;
  }

  .i-switch {
    margin-left: 14px;
    width: 15px;
    height: 15px;
    background-image: url(@/assets/images/common/i-switch.svg);
    background-size: cover;
  }
}
.tab-list {
  .item {
    display: flex;
    margin-top: 10px;
    .grade-name {
      font-size: 16px;
      font-weight: 500;
    }
    .subject-list {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      align-items: center;
      gap: 10px 20px;
      margin-left: 25px;
      .subject {
        font-size: 14px;
        cursor: pointer;
      }
      .active {
        color: @color-brand-checked;
      }
    }
  }
}
</style>
